﻿using System.Linq;

using CodingBlog.Core.Model;
using CodingBlog.Core.Repository.Contract;

namespace CodingBlog.Core.Repository
{
    internal class UserRepository : IUserRepository
    {
        public User CreateUser(User userToBeCreated)
        {
            using (var context = new CodingBlogContext())
            {
                context.Users.Add(userToBeCreated);
                context.SaveChanges();
            }

            return userToBeCreated;
        }

        public User GetUserById(int userId)
        {
            using (var context = new CodingBlogContext())
            {
                return context.Users.SingleOrDefault(x => x.Id == userId);
            }
        }

        public bool EmailIsInUse(string email)
        {
            using (var context = new CodingBlogContext())
            {
                return context.Users.Any(x => x.Email == email);
            }
        }

        public User GetUserByEmailAndPassword(string email, string password)
        {
            using (var context = new CodingBlogContext())
            {
                return context.Users.SingleOrDefault(x => x.Email == email && x.Password == password);
            }
        }
    }
}
